<?php
class User extends AppModel {
    var $name = 'User';
    
    var $belongsTo = array('Group');
    
    
    
   
    
    var $actsAs = array('Acl' => 'requester', 'Containable');
    
    var $validate = array(
        'username' => array(
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'required' => true,
                'message' => 'Alphabets and numbers only'
            ),
            'between' => array(
                'rule' => array('between', 5, 15),
                'message' => 'Between 5 to 15 characters'
            ),
            'unique' => array(
                'rule' => array('isUnique'),
                'message' => 'This username has already been taken'
            )
        ),
        /*'name' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'required' => true,
                'message' =>'please enter your name'
            ),
        
        ),*/
        'password' => array(
            'minLength' => array(
                'rule' => array('minLength', '6'),
                'message' =>'minimum 6 characters'
            ),
            'confirm' => array(
                'rule' => array('identicalFieldValues', 'passwd' ), 
                'message' => 'Please re-enter your password twice so that the values match' 
            )
        ),
        'passwd' => array(
            'rule' => array('minLength', '6'),
            'message' =>'minimum 6 characters'
        ),
        'email' => array(
            'email'=>array(
                'message'=>'This is not a valid email format!',
                'required' => false,
                ),
            'unique' => array(
                'rule' => array('isUnique'),
                'message' => 'This email has already been registered',
                'allowEmpty' => true
            )
        )
    );
    
    
     function parentNode() {
        
        if (!$this->id && empty($this->data)) {
            return null;
        }
        $data = $this->data;
        if (empty($this->data)) {
            $data = $this->read();
        }
        if (!isset($data['User']['group_id'])) {
            return null;
        } else {
            return array('Group' => array('id' => $data['User']['group_id']));
        }
    }
    
    function afterSave($created) {
        if (!$created) {
            
            $parent = $this->parentNode();
            $parent = $this->node($parent);
            $node = $this->node();
            $aro = $node[0];
            $aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
            $this->Aro->save($aro);
        }
    }
    
    
    function identicalFieldValues( $field=array(), $compare_field=null ) { 
        foreach( $field as $key => $value ) :
            $v1 = $value; 
            $v2 = $this->data[$this->name][ $compare_field ];                  
            if($v1 !== $v2) return false; 
            else continue; 
        endforeach;
        return true; 
    } 
    
}
?>
